#include "searchlist.h"
#include"tcpmgr.h"
#include"adduseritem.h"
#include<QWheelEvent>
#include<QScrollBar>
#include<QJsonObject>
#include"findfaildlg.h"
#include"findsuccessdlg.h"
#include"customizeedit.h"
#include"usermgr.h"

SearchList::SearchList(QWidget*parent):QListWidget(parent),_find_dlg(nullptr),
    _search_edit(nullptr),_send_pending(false)
{
    Q_UNUSED(parent);
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    this->viewport()->installEventFilter(this);
    //添加点击的信号槽
    connect(this,&QListWidget::itemClicked,this,&SearchList::slot_item_clicked);

    addTipItem();

    //添加获取用户信息的槽
    connect(TcpMgr::GetInstance().get(),&TcpMgr::sig_user_search,this,&SearchList::slot_user_search);

}

void SearchList::CloseFindDlg()
{
    if(_find_dlg){
        _find_dlg->hide();
        _find_dlg = nullptr;
    }
}

void SearchList::SetSearchEdit(QWidget* edit)
{
    _search_edit=edit;
}

bool SearchList::eventFilter(QObject *watched, QEvent *event)
{
    //鼠标是否进入list
    if(watched==this->viewport()){
        if(event->type()==QEvent::Enter){
            this->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        }
        else if(event->type()==QEvent::Leave){
            this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        }
    }
    //鼠标是否进入滚轮
    if(watched==this->viewport()&&event->type()==QEvent::Wheel){
        QWheelEvent* wheelEvent=static_cast<QWheelEvent*>(event);
        int numDegrees=wheelEvent->angleDelta().y()/8;
        int numSteps=numDegrees/15;

        this->verticalScrollBar()->setValue(this->verticalScrollBar()->value()-numSteps);

    }
    return QListWidget::eventFilter(watched,event);
}

void SearchList::waitPending(bool pending)
{
    if(pending){
        _loading_Dlg=new LoadingDlg(this);
        _loading_Dlg->setModal(true);
        _loading_Dlg->show();
        _send_pending=pending;
    }
    else{
        _loading_Dlg->hide();
        _loading_Dlg->deleteLater();
        _send_pending=pending;
    }
}

void SearchList::addTipItem()
{
    auto* invalid_item=new QWidget();
    QListWidgetItem* item_tmp=new QListWidgetItem;

    item_tmp->setSizeHint(QSize(275,10));
    this->addItem(item_tmp);
    invalid_item->setObjectName("invalid_item");

    this->setItemWidget(item_tmp,invalid_item);
    //设置该项不可被用户选择
    item_tmp->setFlags(item_tmp->flags()& ~Qt::ItemIsSelectable);

    auto* add_user_item=new AddUserItem();
    QListWidgetItem *item=new QListWidgetItem;
    item->setSizeHint(add_user_item->sizeHint());
    this->addItem(item);
    this->setItemWidget(item,add_user_item);
}

void SearchList::slot_item_clicked(QListWidgetItem *item)
{
    QWidget *widget=this->itemWidget(item);
    if(!widget){
        qDebug()<<"slot item clicked widget is nullptr";
        return;
    }
    //将widget转化为基类
    ListItemBase *customItem=qobject_cast<ListItemBase*>(widget);
    if(!customItem){
        qDebug()<<"slot item clicked widget is nullptr";
        return;
    }

    auto itemType=customItem->GetItemType();
    if(itemType==ListItemType::INVALID_ITEM){
        qDebug()<<"slot invalid click";
        return;
    }

    if(itemType==ListItemType::ADD_USER_TIP_ITEM){
        if(_send_pending){
            return;
        }
        if (!_search_edit) {
            return;
        }
        waitPending(true);
        auto search_edit=dynamic_cast<CustomizeEdit*>(_search_edit);

        auto uid_str=search_edit->text();
        QJsonObject jsonObj;
        jsonObj["uid"]=uid_str;

        QJsonDocument doc(jsonObj);
        QByteArray jsonData=doc.toJson(QJsonDocument::Compact);
        emit TcpMgr::GetInstance()->sig_send_data(ReqId::ID_SEARCH_USER_REQ,jsonData);
        return;


    }
    CloseFindDlg();
}

void SearchList::slot_user_search(std::shared_ptr<SearchInfo> s)
{
    waitPending(false);
    if (s == nullptr) {
        _find_dlg = std::make_shared<FindFailDlg>(this);
    }
    else{
        //如果是自己的，则直接返回
        auto self_uid = UserMgr::GetInstance()->GetUid();
        if (s->_uid == self_uid) {
            return;
        }
        //查找是否已经是好友
        bool bExist = UserMgr::GetInstance()->CheckFriendById(s->_uid);
        if(bExist){
            //跳转到聊天界面指定的item中
            emit sig_jump_chat_item(s);
            return;
        }

        _find_dlg = std::make_shared<FindSuccessDlg>(this);
        std::dynamic_pointer_cast<FindSuccessDlg>(_find_dlg)->SetSearchInfo(s);

    }
    _find_dlg->show();
}

